package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.content.TbContent;
import com.dongdongshop.pojo.content_category.TbContentCategory;
import com.dongdongshop.service.content.ContentService;
import com.dongdongshop.service.content_category.ContentCategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("content")
public class ContentController {
    @Reference
    private ContentService contentService;
    @Reference
    private ContentCategoryService contentCategoryService;
    private Logger logger = LoggerFactory.getLogger(ContentController.class);
    //跳转广告分类页面
    @RequestMapping("contentCategoryShow")
    public String toContentCategoryShow(){
        return "admin/content_category";
    }
    //跳转广告页面
    @RequestMapping("contentShow")
    public String ToContentShow(Model model){

        List<TbContentCategory> list = contentCategoryService.listContentCategory();
        model.addAttribute("contentCateList",list);
        return "admin/content";
    }
    //查询广告;
    @RequestMapping("listContent")
    @ResponseBody
    public Result listContent(){
        try {
            List<TbContent> list = contentService.listContent();
            return Result.ok().setData(list);
        }catch (Exception e){
            logger.error("查询错误",e);
            return Result.fail();
        }
    }
    //添加广告
    @RequestMapping("insertContent")
    @ResponseBody
    public Result insertContent(TbContent tbContent){
        try {
            int i = contentService.insertContent(tbContent);
            return Result.ok();
        }catch (Exception e){
            logger.error("添加错误",e);
            return Result.fail();
        }
    }
    //回显数据
    @RequestMapping("getContentById")
    @ResponseBody
    public Result getContent(Long id){
        try {
            TbContent tbContent = contentService.getContentById(id);
            return Result.ok().setData(tbContent);
        }catch (Exception e){
            logger.error("回显错误",e);
            return Result.fail();
        }
    }
    //修改
    @RequestMapping("updateContentById")
    @ResponseBody
    public Result updateContentById(TbContent tbContent){
        try {
            int i = contentService.updateContentById(tbContent);
            return Result.ok();
        }catch (Exception e){
            logger.error("修改错误",e);
            return Result.fail();
        }

    }
    //删除
    @RequestMapping("deleteContent")
    @ResponseBody
    public Result deleteContent(@RequestParam("ids[]")Long[] ids){
        try {
            int i = contentService.deleteContent(ids);
            return Result.ok();
        }catch (Exception e){
            logger.error("删除错误",e);
            return Result.fail();
        }
    }
}
